package com.e3.basdoc.currency.service.impl;

import java.util.List;

import com.e3.basdoc.currency.dao.CurrencyDAO;
import com.e3.basdoc.currency.entity.CurrencyEntity;
import com.e3.basdoc.currency.model.CurrencyModel;
import com.e3.basdoc.currency.service.CurrencyCacheManager;
import com.e3.basdoc.currency.service.CurrencyService;
import com.e3.core.base.DAOHelper;
import com.e3.core.base.ServiceException;
import com.e3.core.base.SuperService;

public class CurrencyServiceImpl extends SuperService implements CurrencyService{
	
	private CurrencyDAO currencyDAO;
	
	private CurrencyDAO getCurrencyDAO(){
		if(currencyDAO == null){
			currencyDAO = DAOHelper.createDAOInstance(CurrencyDAO.class);
		}
		return currencyDAO;
	}

	public CurrencyModel newCurrency(CurrencyModel model) throws ServiceException {
		validateCurrency(model);
		checkCurrencyExisted(model);
		
		CurrencyEntity entity = currencyModelToEntity(model, null);
		getCurrencyDAO().persist(entity);
		
		model.setId(entity.getId());
		
		CurrencyCacheManager.INSTANCE.put(model);
		
		return model;
	}

	public CurrencyModel modifyCurrency(CurrencyModel model) throws ServiceException {
		validateCurrency(model);
		
//		String strId = model.getId();
//		long id = Long.parseLong(strId == null ? "0" : strId);
		long id = model.getId();
		CurrencyEntity entity = getCurrencyDAO().getById(CurrencyEntity.class, id);
		if (entity == null) {
			return newCurrency(model);
		}

		String oldCode = entity.getCode();
		if (!oldCode.equals(model.getCode())) {
			checkCurrencyExisted(model);
		}

		currencyModelToEntity(model, entity);
		getCurrencyDAO().merge(entity);
		
		CurrencyCacheManager.INSTANCE.put(model);
		return model;
	}

	public void deleteCurrencyByIds(List<Long> ids) throws ServiceException {
		if(ids == null||ids.isEmpty()){
			return;
		}
		
		getCurrencyDAO().deleteCurrencyByIds(ids);
		
		CurrencyCacheManager.INSTANCE.remove(ids);
	}

	public void checkCurrencyExisted(CurrencyModel model) throws ServiceException {
		if (model == null) {
			return;
		}

		String code = model.getCode();
		String name = model.getName();
		
		StringBuffer msgs = new StringBuffer();
		long size = getCurrencyDAO().getCurrencySizeByCode(code);
		if(size>0){
			msgs.append("编码为'" + code+"' ");
		}
		size = getCurrencyDAO().getCurrencySizeByName(name);
		if (size>0) {
			msgs.append("名称为'" + name + "' ");
		}
		if(msgs.length()>0){
			msgs.append("已经存在，请设置新的编码和名称。");
			throw new ServiceException(msgs.toString());
		}
	}

	private void validateCurrency(CurrencyModel model) throws ServiceException{
		String code = model.getCode();
		String name = model.getName();
		String sign = model.getSign();

		if (code == null || code.trim().length() == 0 
			|| name == null || name.trim().length() == 0
			|| sign == null || sign.trim().length() == 0	) {
			throw new ServiceException("请设置编码,名称和符号。");
		}
	}
	
	private CurrencyEntity currencyModelToEntity(CurrencyModel model,CurrencyEntity entity){
		entity = modelToEntity(CurrencyEntity.class, model, entity);

		entity.setCode(model.getCode());
		entity.setName(model.getName());
		entity.setSign(model.getSign());
		
		return entity;

	}
}
